import sdk from "../core";
import { ClientManage } from "../core";
import {  ItemType } from "../shared/face/enums";
import { Item } from "../shared/face";

/**
 * 管理员服务类
 * 职责：
 * 1. 系统状态管理
 * 2. 管理员功能操作
 * 3. 测试功能和数据
 * 4. 系统维护操作
 */
export class AdminService {

    /**
     * 获取系统状态
     * @returns 系统状态信息
     */
    static getSystemStatus(): {
        onlineCount: number;
        botConnections: number;
        serverUptime: number;
        memoryUsage: NodeJS.MemoryUsage;
    } {
        return {
            onlineCount: sdk.user.getOnlineCount(),
            botConnections: ClientManage.clientConnMap.size,
            serverUptime: process.uptime(),
            memoryUsage: process.memoryUsage()
        };
    }

    /**
     * 发送系统邮件
     * @param to 收件人ID
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param items 附件
     * @returns 发送结果
     */
    static async sendSystemMail(
        to: string,
        subject: string,
        content: string,
        items: Item[] = []
    ): Promise<{
        success: boolean;
        message: string;
    }> {
        try {
            await sdk.mail.send('sys', to, 1000, subject, content, items);
            return {
                success: true,
                message: '系统邮件发送成功'
            };
        } catch (error) {
            return {
                success: false,
                message: '系统邮件发送失败'
            };
        }
    }

    /**
     * 创建测试物品
     * @param itemType 物品类型
     * @param itemId 物品ID
     * @param count 数量
     * @returns 创建的物品
     */
    static createTestItem(itemType: ItemType, itemId: string, count: number = 1): Item {
        return sdk.prop.createProp({
            id: itemId,
            count: count,
            type: itemType
        });
    }

    /**
     * 强制用户下线
     * @param userId 用户ID
     * @returns 操作结果
     */
    static forceUserOffline(userId: string): {
        success: boolean;
        message: string;
    } {
        try {
            sdk.user.forceOffline(userId);
            return {
                success: true,
                message: '用户已强制下线'
            };
        } catch (error) {
            return {
                success: false,
                message: '强制下线失败'
            };
        }
    }

    /**
     * 执行系统存档
     * @returns 存档结果
     */
    static async performSystemSave(): Promise<{
        success: boolean;
        message: string;
    }> {
        try {
            await sdk.save();
            return {
                success: true,
                message: '系统存档成功'
            };
        } catch (error) {
            return {
                success: false,
                message: '系统存档失败'
            };
        }
    }

    /**
     * 执行系统重启
     * @param delaySeconds 延迟秒数（可选，默认为0表示立即重启）
     * @returns 重启结果
     */
    static async performSystemRestart(delaySeconds: number = 0): Promise<{
        success: boolean;
        message: string;
    }> {
        try {
            // 先存档
            const saveResult = await this.performSystemSave();
            if (!saveResult.success) {
                return saveResult;
            }

            // 存档成功后立即重启（或按指定延迟重启）
            setTimeout(() => {
                process.exit(1);
            }, delaySeconds * 1000);

            const message = delaySeconds > 0 
                ? `系统将在${delaySeconds}秒后重启` 
                : '系统存档完成，立即重启';

            return {
                success: true,
                message
            };
        } catch (error) {
            return {
                success: false,
                message: '系统重启失败'
            };
        }
    }

    /**
     * 发送测试邮件
     * @param recipientId 收件人ID
     * @returns 发送结果
     */
    static async sendTestMail(recipientId: string): Promise<{
        success: boolean;
        message: string;
    }> {
        const timestamp = Date.now();
        const subject = `系统邮件大测试${timestamp}`;
        const content = '这是一封系统测试邮件';
        
        return await this.sendSystemMail(recipientId, subject, content, []);
    }
} 